import time
import json
import datetime

import requests

from django.shortcuts import render
from django.views.generic.base import View

from .models import Play, Warn_model
from .serializers import WarnSerializers
from apps.maps.models import Map
from utils import restful
from utils.clouds.ob_video import CloudVideo, RecVideo, SearchVideo
from utils.user_log import get_user_log
from utils.mixin import UserAuthMixin


class VideoView(UserAuthMixin, View):
    """
    视频监控页
    """
    permission_required = 'video.view_video'

    def get(self, request):
        """
        获取监控点坐标信息
        """
        map_sum = Map.objects.all().count()
        map_good = Map.objects.filter(status=1).count()
        map_fail = Map.objects.filter(status=0).count()
        show_maps = Map.objects.filter(status=0).values('name')

        context = {
            'map_sum': map_sum,
            'map_good': map_good,
            'map_fail': map_fail,
            'show_maps': show_maps
        }
        return render(request, 'video/video.html', context=context)


class VideoHomeView(UserAuthMixin, View):
    """
    视频墙页
    """
    permission_required = 'video.change_video'

    def get(self, request):
        return render(request, 'video/video_home.html')


class WatchVideoView(View):
    def get(self, request):
        return render(request, 'video/view_video.html')


class VideoManageView(UserAuthMixin, CloudVideo, View):
    """
    监控管理页
    """
    permission_required = 'video.change_play'

    def get(self, request):
        """
        获取所有设备并同步云端数据
        :param request:
        :return:
        """
        map_sum = Map.objects.all().count()
        map_good = Map.objects.filter(status=1).count()
        map_fail = Map.objects.filter(status=0).count()
        show_maps = Map.objects.filter(status=0).values('name')

        context = {
            'map_sum': map_sum,
            'map_good': map_good,
            'map_fail': map_fail,
            'show_maps': show_maps
        }
        return render(request, 'video/video_manage.html', context=context)


class RecPlayView(View):
    """
    获取直播播放地址 - rtmp格式
    """

    def post(self, request):
        data = [
            {
                'name': '人脸相机',
                'dev_id': 'rlxj',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/rlxj',
            },
            {
                'name': '守望者',
                'dev_id': 'swz',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/swz',
            },
            {
                'name': '卡扣相机进口独',
                'dev_id': 'kkxjjkd',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/kkxjjkd',
            },
            {
                'name': '卡扣相机进口左',
                'dev_id': 'kkxjjkz',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/kkxjjkz',
            },
            {
                'name': '卡扣相机进口右',
                'dev_id': 'kkxjjky',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/kkxjjky',
            },
            {
                'name': '出口违停球',
                'dev_id': 'ckwtq',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/ckwtq',
            },
            {
                'name': '仓库热成像1',
                'dev_id': 'ckrcx',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/ckrcx',
            },
            {
                'name': '宿舍门口处灵瞳',
                'dev_id': 'ssmk',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/ssmk',
            },
            {
                'name': '北边东道2号',
                'dev_id': 'bbddeh',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/bbddeh',
            },
            {
                'name': '快餐店二1号',
                'dev_id': 'kcde1h',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/kcde1h',
            },
            {
                'name': '南边大厅4号',
                'dev_id': 'nbdt4h',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/nbdt4h',
            },
            {
                'name': '南边大厅一号',
                'dev_id': 'nbdt1h',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/nbdt1h',
            },
            {
                'name': '2-1#消防楼梯',
                'dev_id': '2xflt',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/2xflt',
            },
            {
                'name': '中式快餐二消防',
                'dev_id': 'zskc',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/zskc',
            },
            {
                'name': '美食城车库入口',
                'dev_id': 'mscck',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/mscck',
            },
            {
                'name': '14楼员工中心棋牌',
                'dev_id': 'qpfz',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/qpfz',
            },
            {
                'name': '14楼吧台休息区',
                'dev_id': 'btxx',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/btxx',
            },
            {
                'name': '14F书屋',
                'dev_id': '14fsw',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/14fsw',
            },
            {
                'name': '14楼东北角',
                'dev_id': '14ldbj',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/14ldbj',
            },
            {
                'name': '14楼东侧走道',
                'dev_id': '14ldczd',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/14ldczd',
            },
            {
                'name': '12楼客梯厅',
                'dev_id': '12lktt',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/12lktt',
            },
            {
                'name': '14楼西侧走道',
                'dev_id': '14xczd',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/14xczd',
            },
            {
                'name': '14楼员工中心',
                'dev_id': '14ygzx',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/14ygzx',
            },
            {
                'name': '快餐一店就餐区1',
                'dev_id': 'kc1djc',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/kc1djc',
            },
            {
                'name': '夹1层服务台',
                'dev_id': 'j1cfwt',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/j1cfwt',
            },
            {
                'name': '果汁吧台',
                'dev_id': 'gzbb',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/gzbb',
            },
            {
                'dev_id': 'j3xxq',
                'spread': 'true',
                'rtmp_url': 'rtmp://10.77.81.22:554/live/j3xxq',
            }
        ]

        return restful.result(data=data)


class RecPlay1View(View):
    """
    获取直播播放地址 - rtmp格式
    """

    def get(self, request):
        data = [
            {
                'name': '思华云忆',
                'spread': 'true',
                'children': [
                    {
                        'name': '萧山区',
                        'spread': 'true',
                        'children': [
                            {
                                'name': '公路港(大华平台)',
                                'spread': 'true',
                                'children': [
                                    {
                                        'name': '人脸11相机',
                                        'dev_id': 'rlxj',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/rlxj',
                                    },
                                    {
                                        'name': '守望者',
                                        'dev_id': 'swz',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/swz',
                                    },
                                    {
                                        'name': '卡扣相机进口独',
                                        'dev_id': 'kkxjjkd',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/kkxjjkd',
                                    },
                                    {
                                        'name': '卡扣相机进口左',
                                        'dev_id': 'kkxjjkz',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/kkxjjkz',
                                    }, {
                                        'name': '卡扣相机进口右',
                                        'dev_id': 'kkxjjky',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/kkxjjky',
                                    },
                                    {
                                        'name': '出口违停球',
                                        'dev_id': 'ckwtq',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/ckwtq',
                                    },
                                    {
                                        'name': '仓库热成像1',
                                        'dev_id': 'ckrcx',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/ckrcx',
                                    },
                                    {
                                        'name': '宿舍门口处灵瞳',
                                        'dev_id': 'ssmk',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/ssmk',
                                    },
                                ]
                            },
                            {
                                'name': '传化大厦(海康平台)',
                                'spread': 'true',
                                'children': [
                                    {
                                        'name': '南边大厅4号(录像)',
                                        'cameraid': 'camera1',
                                        'dev_id': 'nbdt4h',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/nbdt4h',
                                    },
                                    {
                                        'name': '12楼客梯厅(录像)',
                                        'cameraid': 'camera2',
                                        'dev_id': '12lktt',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/12lktt',
                                    },
                                    {
                                        'name': '14楼员工中心(录像)',
                                        'cameraid': 'camera3',
                                        'dev_id': 'qpfz',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/qpfz',
                                    },
                                    {
                                        'name': '夹3休息区(录像)',
                                        'cameraid': 'camera4',
                                        'dev_id': 'j3xxq',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/j3xxq',
                                    },
                                    {
                                        'name': '北边东道2号',
                                        'dev_id': 'bbddeh',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/bbddeh',
                                    },
                                    {
                                        'name': '快餐店二1号',
                                        'dev_id': 'kcde1h',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/kcde1h',
                                    },

                                    {
                                        'name': '南边大厅一号',
                                        'dev_id': 'nbdt1h',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/nbdt1h',
                                    },
                                    {
                                        'name': '2-1#消防楼梯',
                                        'dev_id': '2xflt',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/2xflt',
                                    },
                                    {
                                        'name': '中式快餐二消防',
                                        'dev_id': 'zskc',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/zskc',
                                    },
                                    {
                                        'name': '美食城车库入口',
                                        'dev_id': 'mscck',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/mscck',
                                    },

                                    {
                                        'name': '14楼吧台休息区',
                                        'dev_id': 'btxx',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/btxx',
                                    },
                                    {
                                        'name': '14F书屋',
                                        'dev_id': '14fsw',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/14fsw',
                                    },
                                    {
                                        'name': '14楼东北角',
                                        'dev_id': '14ldbj',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/14ldbj',
                                    },
                                    {
                                        'name': '14楼东侧走道',
                                        'dev_id': '14ldczd',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/14ldczd',
                                    },

                                    {
                                        'name': '14楼西侧走道',
                                        'dev_id': '14xczd',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/14xczd',
                                    },
                                    {
                                        'name': '14楼员工中心',
                                        'dev_id': '14ygzx',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/14ygzx',
                                    },
                                    {
                                        'name': '快餐一店就餐区1',
                                        'dev_id': 'kc1djc',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/kc1djc',
                                    },
                                    {
                                        'name': '夹1层服务台',
                                        'dev_id': 'j1cfwt',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/j1cfwt',
                                    },
                                    {
                                        'name': '果汁吧台',
                                        'dev_id': 'gzbb',
                                        'spread': 'true',
                                        'rtmp_url': 'rtmp://10.77.81.22:554/live/gzbb',
                                    },

                                ]
                            }
                        ]
                    }
                ]
            }
        ]

        return restful.result(data=data)


class StopPlayView(RecVideo, View):
    """
    停止播放直播流
    """

    def post(self, request):
        try:
            monitor_id = request.POST.get('monitor_id', '')
            self.stop_play(monitor_id=monitor_id)
        except:
            return restful.result(message='停止失败,请重试.')
        else:
            Play.objects.filter(monitor_id=monitor_id).delete()
            return restful.result()


class PlayVideoView(SearchVideo, View):
    """
    播放录像
    """

    def post(self, request):
        return restful.result()


class StopVideoView(SearchVideo, View):
    """
    停止播放录像
    """

    def post(self, request):
        try:
            monitor_id = request.POST.get('monitor_id', '')
            data = self.stop_video(monitor_id=monitor_id)
        except Exception as e:
            return restful.params_error(message=e)
        return restful.result(data=data)


class SearchVideoView(SearchVideo, View):
    """
    查询录像
    """

    def return_data(self, data):
        times = list(data)
        times.insert(8, 'T')
        times = ''.join(times)
        times = times + '.00Z'
        return times

    def get(self, request):
        """
        前端获取日期，默认只查询一天之间的录像
        :param request:
        :return:
        """
        dev_id = request.GET.get('dev_id', '')
        date_value = request.GET.get('date_value', '')
        start_time = date_value[0:14]
        end_time = date_value[17:31]
        start_time = self.return_data(start_time)
        end_time = self.return_data(end_time)
        data = {
            'rtmp_url': 'http://10.77.81.130:8099/lvj.m3u8?Contentid={0}&owchid=live&starttime={1}&endtime={2}&livemode=4'.format(
                dev_id, start_time, end_time)}
        return restful.result(data=data)


class SaveDataView(View):
    """
    保存ai上传数据
    """

    def post(self, request, model):
        image = request.FILES.get('image', '')
        id = request.POST.get('id', '')
        obj = model()
        obj.id = id
        obj.image = image
        obj.save()
        return restful.result()


class RecVideoView(View):
    def get(self, request):
        return restful.result()


from apps.warnlist.func_select import select_data
from utils.page_func import rec_page


class ReturnDataView(View):
    def get(self, request):
        warns = Warn_model.objects.all().order_by('-add_time')
        warn_sum = warns.count()
        warn_data = rec_page(request, warns)
        serializers = WarnSerializers(warn_data, many=True)
        data = serializers.data
        return restful.result(data=data, count=warn_sum, code=0)
